package cn.edu.henu.team.ui;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.bean.article_brief;
import cn.edu.henu.team.utils.PicturePreviewActivity;

public class DetialActivity extends BaseActivity {

    private String objectId;
    private ImageView back;
    private TextView title;
    private TextView info;
    private ImageView image;
    private TextView content;
    private String img_url;
    private TextView time;
    private SmartRefreshLayout refreshSrl;

    @Override
    protected int initLayout() {
        return R.layout.activity_detial;
    }

    @Override
    protected void initView() {
        Intent it = getIntent();
        objectId = it.getStringExtra("objectId");

        back = findViewById(R.id.dback);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title = findViewById(R.id.dtitle);
        info = findViewById(R.id.dintro);
        image = findViewById(R.id.dimage);
        time = findViewById(R.id.dtime);
        refreshSrl = findViewById(R.id.refresh_srl_detial);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(DetialActivity.this, PicturePreviewActivity.class);
                it.putExtra("url", img_url);
                startActivity(it);
            }
        });
        content = findViewById(R.id.dcontent);

        refreshSrl.setRefreshHeader(new ClassicsHeader(getApplicationContext()));
        refreshSrl.setRefreshFooter(new ClassicsFooter(getApplicationContext()));


        refreshSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshSrl.finishRefresh(1000/*,false*/);
                initData();
            }
        });
        refreshSrl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(1000/*,false*/);//传入false表示加载失败
                initData();
            }
        });
    }

    @Override
    protected void initData() {
        BmobQuery<article_brief> query = new BmobQuery("article_brief");
        query.getObject(objectId, new QueryListener<article_brief>() {
            @Override
            public void done(article_brief object, BmobException e) {
                if (e == null) {
                    title.setText(object.getTitle());
                    img_url = object.getPicture();
                    ImageLoader.getInstance().displayImage(object.getPicture(), image);
                    info.setText(object.getInfo());
                    time.setText(object.getCreatedAt());
                    content.setText(object.getContent());
                } else {
                    Toast.makeText(getApplicationContext(), "文章查询失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


}
